/**
 * @author Arian Stolwijk
 * @license MIT-License, see license.txt
 */

var pageRequest = new Class({
	
	Implements: Events,
	
	initialize: function(page){
		this.page = page; 
		this.page_content = this.page.getFirst();
	},
	
	get: function(){
		var page = this.page;
		var url = this.page.get('title');
		var request = this;
		if (!page.retrieve('loaded') && url) {		
			
			var page_content = this.page_content.set('text','');			
			new Request.HTML({
				url: url,
				
				onRequest: function(){
					page_content
						.adopt(
							$(new pngAnimation('images/gnome-spinner.png',{
								width: 48,
								height: 48,
								rows: 5,
								cols: 7,
								fps: 20
							}))
							.setStyle('margin','0 auto')
						);
					request.fireEvent('onRequest',page);
				},
				
				onSuccess: function(html){
					if (html) {
						page_content.set('tween', {
							property: 'opacity'
						}).get('tween')
							.start(0)
							.chain(function(){
								page_content.set('text','').adopt(html);
								page.store('loaded', true);
								this.start(1);
								// Fire event
								request.fireEvent('onComplete',page);
							});						
					}					
				},
				
				onFailure: function(){
					page_content.set('text', 'Page could not be found! ('+url+')');
					request.fireEvent('onFailure',page);
				}
			}).send();
		}
	}		
});
